package com.pay.mapper;

import com.pay.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    /**
     * 新增账号
     *
     * @param user
     * @return
     */
    @Insert("INSERT INTO user_mail ( email, password, salt, confirm_code, activation_time, is_valid ) " +
            " VALUES ( #{email}, #{password}, #{salt}, #{confirmCode}, #{activationTime}, #{isValid} )")
    int insertUser(User user);

    /**
     * 根据确认码查询用户
     *
     * @param confirmCode
     * @return
     */
    @Select("SELECT email, activation_time FROM user_mail WHERE confirm_code = #{confirmCode} AND is_valid = 0")
    User selectUserByConfirmCode(@Param("confirmCode") String confirmCode);

    /**
     * 根据确认码查询用户并修改状态值为 1（可用）
     *
     * @param confirmCode
     * @return
     */
    @Update("UPDATE user_mail SET is_valid = 1 WHERE confirm_code = #{confirmCode}")
    int updateUserByConfirmCode(@Param("confirmCode") String confirmCode);

    /**
     * 根据邮箱查询用户
     *
     * @param email
     * @return
     */
    @Select("SELECT email, password, salt FROM user_mail WHERE email = #{email} AND is_valid = 1")
    List<User> selectUserByEmail(@Param("email") String email);

}
